﻿Imports InnerLibs
Imports System.Data.OleDb
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Public Class TELA_LOGIN

    Sub login(login As String, senha As String)

        Dim loginreader As SqlDataReader = TELA_START.BANCO.ExecuteSQL("EXEC psLogin'" & login & "', '" & senha.toMD5String & "'")
        If loginreader.HasRows() Then
            loginreader.Read()
            My.Settings.NomeUsuario = loginreader("USU_NOME")
            My.Settings.LoginUsuario = loginreader("USU_LOGIN")
            My.Settings.EmailUsuario = loginreader("USU_EMAIL")
            My.Settings.IdUsuario = loginreader("USU_ID")
            My.Settings.PerfilUsuario = loginreader("PER_NOME")
            My.Settings.idPerfil = loginreader("PER_ID")
            My.Settings.SenhaUsuario = senha
            My.Settings.Save()
            TELA_START.Ribbon1.OrbText = My.Settings.LoginUsuario.ToUpper() & " | " & My.Settings.PerfilUsuario
            Me.Close()
        Else
            MsgBox("Erro ao se conectar!" & vbNewLine & " Verifique usuario e Senha!" & My.Settings.LoginUsuario & vbNewLine & My.Settings.SenhaUsuario, MsgBoxStyle.Exclamation, "Ops, sua princesa está em outro castelo!")
        End If
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        login(UsernameTextBox.Text, PasswordTextBox.Text)
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub TELA_LOGIN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Hide()
        If String.IsNullOrWhiteSpace(My.Settings.LoginUsuario) = False And String.IsNullOrWhiteSpace(My.Settings.SenhaUsuario.Length) = False Then
            login(My.Settings.LoginUsuario, My.Settings.SenhaUsuario)
        Else
            Me.Show()
        End If
    End Sub


End Class
